package com.example.datn.manager;

import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Manager {

	public void viewHtml(){
		Document doc;
		try {
			doc = Jsoup.connect("http://mp3.zing.vn/nghe-si/Quang-Le/bai-hat").get();			
			Elements links = doc.select("ul");
			for (Element link : links) {				 
				if(link.hasClass("song-list")){				
					Elements divs = link.select("li");
					for (Element div : divs) {
						Elements as = div.select("a");
						for (Element a : as) {
							if(a.hasClass("_strCut")){
								if(a.attr("strlength").equals("35")){
									System.out.println("song name: " + a.text());
									String urlSong = "http://mp3.zing.vn" + a.attr("href");
									System.out.println("link song: " + urlSong);	
									System.out.println("link audio: " + getXMLSong(urlSong));
								}									
								else
									System.out.println("album name: " + a.text());
							}
							if(a.hasClass("ln-bar")){								
								Pattern p = Pattern.compile("[0-9]+");
								Matcher m = p.matcher(a.attr("title"));
								if (m.find()) {
								    int n = Integer.parseInt(m.group());
								    System.out.println("luot nghe : " + n);
								}
							}
						}
						System.out.println("----------------");
					}
					break;
				}	
			}
			System.out.println("\n============================================");
		} catch (IOException e) {		
			e.printStackTrace();
		}		
	}
	
	public void getAllLink(){
		try {
			Document doc;
			// need http protocol
			doc = Jsoup.connect("http://mp3.zing.vn/nghe-si/Quang-Le/album").get();
	 
			// get page title
			String title = doc.title();
			System.out.println("title : " + title);
	 
			// get all links
			Elements links = doc.select("a[href]");
			for (Element link : links) {
	 
				// get the value from href attribute
				System.out.println("\nlink : " + link.attr("href"));
				System.out.println("text : " + link.text());	 
			}
	 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public void getLinksImage(){
		Document doc;
		try {			 
			//get all images
			doc = Jsoup.connect("http://mp3.zing.vn/nghe-si/Quang-Le/album").get();
			Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]");
			for (Element image : images) {
	 
				System.out.println("\nsrc : " + image.attr("src"));
				System.out.println("height : " + image.attr("height"));
				System.out.println("width : " + image.attr("width"));
				System.out.println("alt : " + image.attr("alt"));	 
			}
	 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getXMLSong(String url){
		String link = "khong co";
		try {			
			Document doc;						
			//doc = Jsoup.connect("http://mp3.zing.vn/bai-hat/Doi-Mat-Nguoi-Xua-Quang-Le/ZWZBA0FA.html").get();
			doc = Jsoup.connect(url).get();
			
			String a = doc.toString(),b;								
			Pattern pattern = Pattern.compile("xmlURL=([^&]*)");
			Matcher matcher = pattern.matcher(a);
			if (matcher.find())
			{
			    b = matcher.group(1);			    			   
			    doc = Jsoup.connect(b).get();				
				pattern = Pattern.compile("</source>(.+?)=");
				matcher = pattern.matcher(doc.toString());
				if (matcher.find()){
					link = matcher.group(1);
				}
			}				
		} catch (IOException e) {
			e.printStackTrace();
		}		
		return link;
	}
	
}
